package com.wlr.study.leetcode;

/**
 * @author lerong.wang
 * @version 1.0.0
 * @description
 * @date 2025/6/5 17:06
 */
public class LeetCode1343 {

    /**
     * 给你一个整数数组 arr 和两个整数 k 和 threshold 。
     * <p>
     * 请你返回长度为 k 且平均值大于等于 threshold 的子数组数目
     *
     * @param arr       数组
     * @param k         长度
     * @param threshold 阈值
     * @return
     */
    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int sum = 0;
        int result = 0;
        for (int i = 0; i < k; i++) {
            sum += arr[i];
        }
        double avg = 1.0 * sum / k;
        if (avg >= threshold) {
            result++;
        }
        for (int i = k; i < arr.length; i++) {

            sum = sum - arr[i - k] + arr[i];
            avg = 1.0 * sum / k;
            if (avg >= threshold) {
                result++;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        LeetCode1343 leetCode1343 = new LeetCode1343();
        System.out.println(leetCode1343.numOfSubarrays(new int[]{2, 2, 2, 2, 5, 5, 5, 8}, 3, 4));
    }

}